package com.ssm.controller;

import com.ssm.pojo.ProductPojo;
import com.ssm.service.ProductService;
import com.ssm.util.PageUtil;
import javafx.scene.input.DataFormat;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping("/product")
//@RequestMapping()
public class ProductController {
    @Autowired
    @Qualifier("ProductServiceImpl")
    private ProductService productService;

    //每页显示数量
    private static final int rows=12;

    @RequestMapping("/allProduct")
    public String ListProduct(Model model){
        List<ProductPojo> productPojos = productService.selectAllProduct();
        model.addAttribute("productList",productPojos);
        //return "productList";
        //return "FreezeTitle";
        return "SelectTest";
        //return "fSelect";
    }
    @RequestMapping("/splitPageProduct")
    public String splitPageProduct(Model model, HttpServletRequest req, HttpServletResponse resp, @RequestParam(required=false, defaultValue="1") int page){
        int total = productService.selectAllProductCount();
        String pageTool = PageUtil.getPageTool(req,total,page,rows);
        int beginRow=(page-1)*rows;
        List<ProductPojo> productPojos = productService.selectAllProductLimit(beginRow, rows);
        model.addAttribute("productList",productPojos);
        model.addAttribute("pageTool",pageTool);
        return "productList";
    }

    //筛选，去重查询
    @RequestMapping("/selectDistinct")
    public String selectDictinctProduct(Model model,HttpServletRequest req,HttpServletResponse resp,@RequestParam(required=false, defaultValue="1") int page){
        List<String> pidlist = productService.selectDistinctPid();
        List<String> pnamelist = productService.selectDistinctPname();
        List<String> marketpricelist = productService.selectDistinctMarketPrice();
        List<String> pricelist = productService.selectDistinctPrice();
        List<String> pdatelist = productService.selectDistinctPdate();
        List<String> ishotlist = productService.selectDistinctIsHot();
        List<String> flaglist = productService.selectDistinctFlag();
        List<String> cidlist = productService.selectDistinctCid();
        int total = productService.selectAllProductCount();
        String pageTool = PageUtil.getPageTool(req,total,page,rows);
        int beginRow=(page-1)*rows;
        List<ProductPojo> productPojos = productService.selectAllProductLimit(beginRow, rows);
        model.addAttribute("pidlist",pidlist);
        model.addAttribute("pnamelist",pnamelist);
        model.addAttribute("marketpricelist",marketpricelist);
        model.addAttribute("pricelist",pricelist);
        model.addAttribute("pdatelist",pdatelist);
        model.addAttribute("ishotlist",ishotlist);
        model.addAttribute("flaglist",flaglist);
        model.addAttribute("cidlist",cidlist);
        model.addAttribute("productList",productPojos);
        model.addAttribute("pageTool",pageTool);
        return "SelectProductList";
    }

    //筛选，去重查询改造
    @RequestMapping("/selectDistinct2")
    public String selectDictinctProduct2(Model model,HttpServletRequest req,HttpServletResponse resp,@RequestParam(required=false, defaultValue="1") int page){
        int total = productService.selectAllProductCount();
        String pageTool = PageUtil.getPageTool(req,total,page,rows);
        int beginRow=(page-1)*rows;
        List<ProductPojo> productPojos = productService.selectAllProductLimit(beginRow, rows);
        HashMap<String,String> pidMap = new HashMap<String, String>();
        HashMap<String,String> pnameMap = new HashMap<String, String>();
        HashMap<String,String> marketpriceMap = new HashMap<String, String>();
        HashMap<String,String> priceMap = new HashMap<String, String>();
        HashMap<String,String> pdateMap = new HashMap<String, String>();
        HashMap<String,String> ishotMap = new HashMap<String, String>();
        HashMap<String,String> flagMap = new HashMap<String, String>();
        HashMap<String,String> cidMap = new HashMap<String, String>();
        for(ProductPojo productpojo:productPojos){
            if(!pidMap.containsKey(productpojo.getPid())){
                pidMap.put(productpojo.getPid(),"");
            }
            if(!pnameMap.containsKey(productpojo.getPname())){
                pnameMap.put(productpojo.getPname(),"");
            }
            if(!marketpriceMap.containsKey(productpojo.getMarket_price())){
                marketpriceMap.put(productpojo.getMarket_price().toString(),"");
            }
            if(!priceMap.containsKey(productpojo.getShop_price())){
                priceMap.put(productpojo.getShop_price().toString(),"");
            }
            if(!pdateMap.containsKey(productpojo.getPdate())){
                pdateMap.put(productpojo.getPdate().toString(),"");
            }
            if(!ishotMap.containsKey(productpojo.getIs_hot())){
                ishotMap.put(String.valueOf(productpojo.getIs_hot()),"");
            }
            if(!flagMap.containsKey(productpojo.getPflag())){
                flagMap.put(String.valueOf(productpojo.getPflag()),"");
            }
            if(!cidMap.containsKey(productpojo.getCid())){
                cidMap.put(productpojo.getCid(),"");
            }
        }
        System.out.println("pidMap------------"+pidMap);
        model.addAttribute("pidMap",pidMap);
        model.addAttribute("pnameMap",pnameMap);
        model.addAttribute("marketpriceMap",marketpriceMap);
        model.addAttribute("priceMap",priceMap);
        model.addAttribute("pdateMap",pdateMap);
        model.addAttribute("ishotMap",ishotMap);
        model.addAttribute("flagMap",flagMap);
        model.addAttribute("cidMap",cidMap);
        model.addAttribute("productList",productPojos);
        model.addAttribute("pageTool",pageTool);
        return "SelectProductList2";
    }

    //筛选去重查询，不带分页展示
    @RequestMapping("/selectDistinct3")
    public String selectDictinctProduct3(Model model,HttpServletRequest req,HttpServletResponse resp){
        List<ProductPojo> productPojos = productService.selectAllProduct();
        HashMap<String,String> pidMap = new HashMap<String, String>();
        HashMap<String,String> pnameMap = new HashMap<String, String>();
        HashMap<String,String> marketpriceMap = new HashMap<String, String>();
        HashMap<String,String> priceMap = new HashMap<String, String>();
        HashMap<String,String> pdateMap = new HashMap<String, String>();
        HashMap<String,String> ishotMap = new HashMap<String, String>();
        HashMap<String,String> flagMap = new HashMap<String, String>();
        HashMap<String,String> cidMap = new HashMap<String, String>();
        for(ProductPojo productpojo:productPojos){
            if(!pidMap.containsKey(productpojo.getPid())){
                pidMap.put(productpojo.getPid(),"");
            }
            if(!pnameMap.containsKey(productpojo.getPname())){
                pnameMap.put(productpojo.getPname(),"");
            }
            if(!marketpriceMap.containsKey(productpojo.getMarket_price())){
                marketpriceMap.put(productpojo.getMarket_price().toString(),"");
            }
            if(!priceMap.containsKey(productpojo.getShop_price())){
                priceMap.put(productpojo.getShop_price().toString(),"");
            }
            if(!pdateMap.containsKey(productpojo.getPdate())){
                pdateMap.put(productpojo.getPdate().toString(),"");
            }
            if(!ishotMap.containsKey(productpojo.getIs_hot())){
                ishotMap.put(String.valueOf(productpojo.getIs_hot()),"");
            }
            if(!flagMap.containsKey(productpojo.getPflag())){
                flagMap.put(String.valueOf(productpojo.getPflag()),"");
            }
            if(!cidMap.containsKey(productpojo.getCid())){
                cidMap.put(productpojo.getCid(),"");
            }
        }
        System.out.println("pidMap------------"+pidMap);
        model.addAttribute("pidMap",pidMap);
        model.addAttribute("pnameMap",pnameMap);
        model.addAttribute("marketpriceMap",marketpriceMap);
        model.addAttribute("priceMap",priceMap);
        model.addAttribute("pdateMap",pdateMap);
        model.addAttribute("ishotMap",ishotMap);
        model.addAttribute("flagMap",flagMap);
        model.addAttribute("cidMap",cidMap);
        model.addAttribute("productList",productPojos);
        return "SelectProductList3";
    }

    //筛选，form表单筛选提交
    @RequestMapping("/formSelect3")
    public String formSelect(Model model,HttpServletRequest req,HttpServletResponse resp,
        @RequestParam("pidlist_") String pidstr,
        @RequestParam("pnamelist_") String pnamestr,
        @RequestParam("marketpricelist_") String marketpricestr,
        @RequestParam("pricelist_") String pricestr,
        @RequestParam("pdatelist_") String pdatestr,
        @RequestParam("ishotlist_") String ishotstr,
        @RequestParam("flaglist_") String flagstr,
        @RequestParam("cidlist_") String cidstr
        ){
        System.out.println("PidList----"+pidstr);
        //PidList----11,1,12,13
        //string类型的拼接''，其他的不拼接
        pidstr = pidstr.replace(",","','");
        pidstr = "'"+pidstr+"'";
        pnamestr = pnamestr.replace(",","','");
        pnamestr = "'"+pnamestr+"'";
        pdatestr = pdatestr.replace(",","','");
        pdatestr = "'"+pdatestr+"'";
        cidstr = cidstr.replace(",","','");
        cidstr = "'"+cidstr+"'";
        System.out.println("PidList=========="+pidstr);

        //调用查询方法，进行查询
        Map<String,String> map = new HashMap<String, String>();
        map.put("pidstr",pidstr);
        map.put("pnamestr",pnamestr);
        map.put("marketpricestr",marketpricestr);
        map.put("pricestr",pricestr);
        map.put("pdatestr",pdatestr);
        map.put("ishotstr",ishotstr);
        map.put("flagstr",flagstr);
        map.put("cidstr",cidstr);
        System.out.println("strMap+++++++++++++"+map);
        //List<ProductPojo> selectProductpojos = productService.selectForm(strMap);
        List<ProductPojo> selectProductpojos = productService.selectForm2(map);

        /*-------------------------------------------------------*/
        List<ProductPojo> productPojos = productService.selectAllProduct();
        HashMap<String,String> pidMap = new HashMap<String, String>();
        HashMap<String,String> pnameMap = new HashMap<String, String>();
        HashMap<String,String> marketpriceMap = new HashMap<String, String>();
        HashMap<String,String> priceMap = new HashMap<String, String>();
        HashMap<String,String> pdateMap = new HashMap<String, String>();
        HashMap<String,String> ishotMap = new HashMap<String, String>();
        HashMap<String,String> flagMap = new HashMap<String, String>();
        HashMap<String,String> cidMap = new HashMap<String, String>();
        for(ProductPojo productpojo:productPojos){
            if(!pidMap.containsKey(productpojo.getPid())){
                pidMap.put(productpojo.getPid(),"");
            }
            if(!pnameMap.containsKey(productpojo.getPname())){
                pnameMap.put(productpojo.getPname(),"");
            }
            if(!marketpriceMap.containsKey(productpojo.getMarket_price())){
                marketpriceMap.put(productpojo.getMarket_price().toString(),"");
            }
            if(!priceMap.containsKey(productpojo.getShop_price())){
                priceMap.put(productpojo.getShop_price().toString(),"");
            }
            if(!pdateMap.containsKey(productpojo.getPdate())){
                /*DateFormat dataFormat = DateFormat.getDateInstance(DateFormat.SHORT, Locale.CHINA);*/
                pdateMap.put(productpojo.getPdate().toString(),"");
            }
            if(!ishotMap.containsKey(productpojo.getIs_hot())){
                ishotMap.put(String.valueOf(productpojo.getIs_hot()),"");
            }
            if(!flagMap.containsKey(productpojo.getPflag())){
                flagMap.put(String.valueOf(productpojo.getPflag()),"");
            }
            if(!cidMap.containsKey(productpojo.getCid())){
                cidMap.put(productpojo.getCid(),"");
            }
        }
        System.out.println("pidMap------------"+pidMap);
        model.addAttribute("pidMap",pidMap);
        model.addAttribute("pnameMap",pnameMap);
        model.addAttribute("marketpriceMap",marketpriceMap);
        model.addAttribute("priceMap",priceMap);
        model.addAttribute("pdateMap",pdateMap);
        model.addAttribute("ishotMap",ishotMap);
        model.addAttribute("flagMap",flagMap);
        model.addAttribute("cidMap",cidMap);
        /* -------------------------------------------------------*/

        model.addAttribute("productList",selectProductpojos);
        return "SelectProductList3";
    }
    //拆分select选中的option
    public List<String> createList(String arr[],List<String> list){
        for(int i=0;i<arr.length;i++){
            list.add(arr[i]);
        }
        return list;
    }
}
